package states
{
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;
	
	import utilities.*;
	
	/**
	 * ...
	 * @author JAC
	 */
	public class ScoreState extends PlayState
	{
		private static var _overlay:FlxGroup;
		private static var _fireworks:Emitter;
		private static var _fireworkTimer:FlxDelay;
		private static var _statTexts:Array;
		
		private static var _menuGrave:Entity;
		private static var _replayGrave:Entity;
		private static var _nextGrave:Entity;
		
		private var _replayLevel:Class;
		private var _nextLevel:Class;
		private var _stateToSwitchTo:Class;
		private var _levelGrave:LevelGrave;
		
		public function ScoreState(lg:LevelGrave = null):void
		{
			if (lg != null)
			{
				_replayLevel = lg.level;
				_nextLevel = lg.next == null ? CreditState : lg.next.level;
				if (Tracker.levelComplete && lg.state == LevelGrave.UNLOCKED)
				{
					lg.setState(LevelGrave.CLEARED);
					if (lg.next != null)
					{
						lg.next.setState(LevelGrave.UNLOCKED);
						Registry.saveProgress(lg.next.level);
					}
				}
			}
			else
			{
				_replayLevel = MenuState;
				_nextLevel = MenuState;
			}
			
			_levelGrave = lg;
			checkGraves();
		}
		
		override public function create():void
		{
			super.create();
			
			includeLayer(_misc);
			includeLayer(_animations);
			
			_misc.add(Registry.epicBackground);
			Registry.epicBackground.setAll("alpha", 0.1);
			
			_maze.load(MazeData.score);
			Player.spawnAt(14, 19);
			
			_misc.add(_overlay);
			_misc.add(_fireworks);
			_misc.add(_menuGrave);
			_misc.add(_replayGrave);
			_misc.add(_nextGrave);
			
			initStats();
			
			_fireworks.exists = Tracker.levelComplete;
			
			// Clear data from here
			Tracker.init();
			
			_ui.resetText();
			_ui.clearInventory();
			
			MusicPlayer.play(MusicPlayer.TOMB);
		}
		
		override public function destroy():void
		{
			Registry.epicBackground.setAll("alpha", 1.0);
			
			_misc.clear();
			
			super.destroy();
		}
		
		override public function update():void
		{
			super.update();
			
			if (FlxG.overlap(_player, _menuGrave))
			{
				_menuGrave.say("overlap");
				if (FlxG.keys.justPressed("SPACE"))
				{
					FlxG.switchState(new MenuState());
				}
			}
			
			if (FlxG.overlap(_player, _replayGrave))
			{
				_replayGrave.say("overlap");
				if (FlxG.keys.justPressed("SPACE"))
				{
					_stateToSwitchTo = _replayLevel;
					FlxG.fade(0xFF000000, 1, onFade);
				}
			}
			
			if (_levelGrave != null && _levelGrave.next != null)
			{
				if (FlxG.overlap(_player, _nextGrave))
				{
					if (_levelGrave.next.state != LevelGrave.LOCKED)
					{
						_nextGrave.say("overlap");
						if (FlxG.keys.justPressed("SPACE"))
						{
							_stateToSwitchTo = _nextLevel;
							FlxG.fade(0xFF000000, 1, onFade);
						}
					}
					else
					{
						_nextGrave.say("locked");
					}
				}
			}
			
			if (_nextLevel == CreditState)
			{
				if (FlxG.overlap(_player, _nextGrave))
				{
					if (FlxG.keys.justPressed("SPACE"))
					{
						_stateToSwitchTo = _nextLevel;
						FlxG.fade(0xFF000000, 1, onFade);
					}
				}
			}
		
			//fireworks();
		}
		
		private function initStats():void
		{
			var stats:Array = new Array();
			stats.push(Tracker.levelComplete ? Tracker.mapScore.toString() : "NO");
			stats.push(null);
			stats.push(null);
			stats.push(null);
			stats.push(Tracker.score.toString() + " x 500");
			stats.push(Tracker.enemiesDefeated.toString() + " x 250");
			stats.push(Tracker.lives.toString() + " x 1000");
			
			var text:FlxText;
			text = new FlxText(225, 75, 275, "LEVEL " + _levelGrave.ID + " COMPLETED");
			text.font = "cooper_black";
			text.color = 0xFF000000;
			text.size = 15;
			_misc.add(text);
			
			text = new FlxText(225, 75, 275, stats[0]);
			text.font = "cooper_black";
			text.color = 0xFF000000;
			text.size = 15;
			text.alignment = "right";
			_misc.add(text);
			
			for (var i:uint = 1; i < _statTexts.length; i++)
			{
				text = new FlxText(225, 75 + i * 30, 275, _statTexts[i]);
				text.font = "cooper_black";
				text.color = 0xFF000000;
				text.size = 15;
				_misc.add(text);
				
				if (stats[i] != null)
				{
					text = new FlxText(225, 75 + i * 30, 275, stats[i]);
					text.font = "cooper_black";
					text.color = 0xFF000000;
					text.size = 15;
					text.alignment = "right";
					_misc.add(text);
				}
			}
			
			var actualScore:int = Tracker.levelComplete ? Tracker.mapScore : 0;
			actualScore += Tracker.score * 500 + Tracker.enemiesDefeated * 250 + Tracker.lives * 1000;
			
			if (actualScore > _levelGrave.highScore)
			{
				_levelGrave.setScore(actualScore);
			}
			
			var yourHighScoreText:FlxText = new FlxText(225, 345, 275, "HIGH SCORE:");
			yourHighScoreText.font = "cooper_black";
			yourHighScoreText.size = 15;
			yourHighScoreText.color = 0xFF000000;
			_misc.add(yourHighScoreText);
			
			var highScoreText:FlxText = new FlxText(225, 345, 275, _levelGrave.highScore.toString());
			highScoreText.font = "cooper_black";
			highScoreText.size = 15;
			highScoreText.alignment = "right";
			highScoreText.color = 0xFF000000;
			_misc.add(highScoreText);
			
			var yourScoreText:FlxText = new FlxText(225, 375, 275, "SCORE:");
			yourScoreText.font = "cooper_black";
			yourScoreText.size = 15;
			yourScoreText.color = 0xFF000000;
			_misc.add(yourScoreText);
			
			var scoreText:FlxText = new FlxText(225, 375, 275, actualScore.toString());
			scoreText.font = "cooper_black";
			scoreText.size = 15;
			scoreText.alignment = "right";
			scoreText.color = 0xFF000000;
			_misc.add(scoreText);
		}
		
		private function checkGraves():void
		{
			if (_levelGrave != null)
			{
				if (_levelGrave.state == LevelGrave.CLEARED)
				{
					_replayGrave.loadGraphic(Assets.ImgGraveCleared);
					_nextGrave.loadGraphic(Assets.ImgGraveUnlocked, true);
					_nextGrave.addAnimation("glow", [0, 1, 2], 3);
					_nextGrave.play("glow");
				}
				else
				{
					_replayGrave.loadGraphic(Assets.ImgGraveUnlocked, true);
					_replayGrave.addAnimation("glow", [0, 1, 2], 3);
					_replayGrave.play("glow");
					_nextGrave.loadGraphic(Assets.ImgGraveLocked);
				}
			}
			else
			{
				_replayGrave.loadGraphic(Assets.ImgGrave);
				_nextGrave.loadGraphic(Assets.ImgGrave);
			}
			
			_replayGrave.width = 25;
			_replayGrave.height = 25;
			_replayGrave.centerOffsets();
			
			_nextGrave.width = 25;
			_nextGrave.height = 25;
			_nextGrave.centerOffsets();
		}
		
		private function fireworks():void
		{
			if (!_fireworks.exists)
			{
				return;
			}
			
			if (_fireworkTimer.hasExpired)
			{
				_fireworks.x = 25 + FlxU.floor(FlxG.random() * 675);
				_fireworks.y = 25 + FlxU.floor(FlxG.random() * 475);
				_fireworks.start(true, 5);
				_fireworkTimer.start();
			}
		}
		
		private function onFade():void
		{
			Registry.paused = false;
			FlxG.switchState(new _stateToSwitchTo());
		}
		
		public static function init():void
		{
			initOverlay();
			initFireworks();
			initGraves();
			initStatTexts();
		}
		
		private static function initOverlay():void
		{
			_overlay = new FlxGroup();
			
			var overlayBGBorder:FlxSprite = new FlxSprite(190, 40);
			overlayBGBorder.makeGraphic(345, 395, 0x80123456);
			_overlay.add(overlayBGBorder);
			
			var overlayBG:FlxSprite = new FlxSprite(200, 50);
			overlayBG.makeGraphic(325, 375, 0xFFCCCCCC);
			_overlay.add(overlayBG);
		}
		
		private static function initFireworks():void
		{
			_fireworkTimer = new FlxDelay(1000);
			_fireworkTimer.start();
			
			_fireworks = new Emitter(0, 0, 200);
			_fireworks.createParticles(2, 2, 0xAAFFFF42);
			_fireworks.maxParticleSpeed.make(150, 150);
			_fireworks.minParticleSpeed.make(-150, -150);
		}
		
		private static function initGraves():void
		{
			_menuGrave = new Entity(11, 19);
			_menuGrave.loadGraphic(Assets.ImgGrave);
			_menuGrave.addText("overlap", "Main menu", 80, 3);
			
			_replayGrave = new Entity(14, 18);
			_replayGrave.loadGraphic(Assets.ImgGrave);
			_replayGrave.addText("overlap", "Replay level", 80, 3);
			
			_nextGrave = new Entity(17, 19);
			_nextGrave.loadGraphic(Assets.ImgGrave);
			_nextGrave.addText("overlap", "Next level", 80, 3);
			_nextGrave.addText("locked", "LOCKED!", 80, 3);
		}
		
		private static function initStatTexts():void
		{
			_statTexts = new Array();
			_statTexts.push("LEVEL");
			_statTexts.push("");
			_statTexts.push("BONUS");
			_statTexts.push("---------------------------------------------------------------");
			_statTexts.push("TREASURES SAVED");
			_statTexts.push("ENEMIES DEFEATED");
			_statTexts.push("EXTRA LIVES");
		}
	}
}